package com.navinfo.tripanalysis.common.arithmetic.custom;

import com.navinfo.tripanalysis.common.arithmetic.NumberBy0200;
import com.navinfo.tripanalysis.common.arithmetic.SharpTurning;
import lombok.Data;

/**
 * 根据状态位进行次数累计的统计变量
 * {@link NumberBy0200}
 * {@link SharpTurning}
 * @author 沈东生
 */
@Data
public class VarCalcNumber {
    /**
     * 每个状态位，上一个点的状态数组
     */
    private boolean[] preStatus;
    /**
     * 每个状态位，次数数组
     */
    private int[] number;

    /**
     * 初始化或获取状态位
     * @param size
     * @return
     */
    public VarCalcNumber init(int size) {
        if (preStatus == null) {
            preStatus = new boolean[size];
        }
        if (number == null) {
            number = new int[size];
        }
        return this;
    }

    /**
     * 计算状态位是否改变
     * @param aryStatus
     */
    public void calc(Boolean[] aryStatus) {
        this.init(aryStatus.length);
        for (int i=0; i<aryStatus.length; i++) {
            Boolean status = aryStatus[i];
            if (status != null) {
                boolean curState = status;
                boolean preState = preStatus[i];
                if (!preState && curState) {
                    number[i] = number[i] + 1;
                }

                preStatus[i] = curState;
            }
        }
    }
}
